package com.athuawei.lock;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class ShareResource {
    // 定义标识位AA:1 BB:2 CC:3
    private int flag = 1;

    // 创建lock锁
    private Lock lock = new ReentrantLock();

    //创建3个condition
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();

    //打印5次，参数第几轮
    public void print5(int loop) throws InterruptedException {
        //上锁
        lock.lock();
        try{
            //判断
            while(flag != 1){
                condition1.await();
            }
            //干活
            for(int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName()
                +" :: " + i + " :轮数" + loop);
            }
            //通知
            flag = 2;
            condition2.signalAll(); // 通知BB
        } finally {
            lock.unlock();
        }
    }
    //打印10次，参数第几轮
    public void print10(int loop) throws InterruptedException {
        //上锁
        lock.lock();
        try{
            //判断
            while(flag != 2){
                condition2.await();
            }
            //干活
            for(int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName()
                        +" :: " + i + " :轮数" + loop);
            }
            //通知
            flag = 3;
            condition3.signalAll(); // 通知CC
        } finally {
            lock.unlock();
        }
    }
    //打印15次，参数第几轮
    public void print15(int loop) throws InterruptedException {
        //上锁
        lock.lock();
        try{
            //判断
            while(flag != 3){
                condition3.await();
            }
            //干活
            for(int i = 0; i < 15; i++) {
                System.out.println(Thread.currentThread().getName()
                        +" :: " + i + " :轮数" + loop);
            }
            //通知
            flag = 1;
            condition1.signalAll(); // 通知AA
        } finally {
            lock.unlock();
        }
    }
}

public class ThreadDemo3 {

    public static void main(String[] args) {
        ShareResource shareResource = new ShareResource();
        new Thread(()->{
            for(int i = 0; i <= 10; i++) {
                try {
                    shareResource.print5(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"AA").start();

        new Thread(()->{
            for(int i = 0; i <= 10; i++) {
                try {
                    shareResource.print10(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"BB").start();

        new Thread(()->{
            for(int i = 0; i <= 10; i++) {
                try {
                    shareResource.print15(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"CC").start();
    }
}
